require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

require(['jquery', 'doT', 'common', 'bootstrap', 'paginator'], function($, doT, common) {
    $(function() {
        var pageSize = 10;
        var schoolList;
        var role = sessionStorage.getItem('role');
        if (role == 1) {
            $('.managerOnly').removeClass('managerOnly');
        }

        function initPaginator(paginator, currentPage, numberOfPages, totalPages, event) {
            var options = {
                currentPage: currentPage,
                numberOfPages: numberOfPages,
                totalPages: totalPages,
                itemTexts: function(type, page, current) {
                    switch (type) {
                        case 'first':
                            return '首页';
                        case 'prev':
                            return '上一页';
                        case 'next':
                            return '下一页';
                        case 'last':
                            return '末页';
                        case 'page':
                            return page;
                    }
                },
                onPageChanged: event
            };
            paginator.bootstrapPaginator(options);
        }

        function refreshSchoolList(p) {
            var page = p || 1;
            $.post(common.urlBase + '/school/desktop/allSchool.json', { page: page, rows: pageSize }, function(result) {
                var schoolTmpl = doT.template($('#schoolTmpl').text());
                schoolList = result.data;
                $('#schoolList').html(schoolTmpl(schoolList.obj));
                if (role == 1) {
                    $('.managerOnly').removeClass('managerOnly');
                }
                var total = schoolList.total_result;
                $('#total').text(total);
                var pageNumber = Math.ceil(total / pageSize);
                initPaginator($('#paginator'), page, 8, pageNumber, SchoolListChangePage);
            }, 'json');
        }

        function SchoolListChangePage(event, oldPage, newPage) {
            refreshSchoolList(newPage);
        }

        refreshSchoolList();

        $(document).on('click', '.deleteSchool', function(event) {
            var school_id = $(event.currentTarget).attr('data-id');
            console.log($(event.currentTarget));
            $('#confirmMsg').text('确认删除该学校吗?');
            $('#confirmBtn').on('click', function(event) {
                $.getJSON(common.urlBase + '/school/deleteSchool/' + school_id + '.json', function(result) {
                    if (result.ok) {
                        $('#noticeMsg').text('删除成功!');
                        $('#noticeModal').modal('show');
                        refreshSchoolList();
                    } else {
                        $('#noticeMsg').text('删除失败!');
                        $('#noticeModal').modal('show');
                    }
                });
                $('#confirmModal').modal('hide');
            });
            $('#confirmModal').modal('show');
        });

    });
});
